package com.tsmti.webservice.custom.standard;

import com.alibaba.fastjson.JSON;
import com.tsmti.webservice.constants.Constants;
import com.tsmti.webservice.custom.standard.impl.HisImpl;
import com.tsmti.webservice.dto.param.DepartmentParamDto;
import com.tsmti.webservice.dto.param.DoctorParamDto;
import com.tsmti.webservice.dto.param.PatientParamDto;
import com.tsmti.webservice.dto.result.DepartmentResultDto;
import com.tsmti.webservice.dto.result.DoctorResultDto;
import com.tsmti.webservice.dto.result.PatientResultDto;
import com.tsmti.webservice.entity.ReturnMsg;
import com.tsmti.webservice.exception.ErrorCodeEnums;
import com.tsmti.webservice.exception.WebServiceException;
import com.tsmti.webservice.util.StringUtils;
import org.apache.log4j.Logger;
import java.util.List;

/**
 * @Package：com.tsmti.webservice.custom.standard
 * @Class：PimsServiceImpl
 * @Description： 测试接口
 * @Author：king
 * @Date：Created in 2018/11/15 14:58
 * @Company:
 * @Version：
 * @Modified By:
 */
public class PimsServiceImpl {
    private static Logger log = Logger.getLogger(PimsServiceImpl.class);
    /***
     * 1001:根据住院号/就诊号获取病人信息接口
     * @param param 参数
     * @return 根据住院号/就诊号获取病人信息接口
     */
    public ReturnMsg getPatientInfoById(String param){
        ReturnMsg msg = ReturnMsg.success(Constants.YesNo.NO,"获取病人信息失败");
        try {
            PatientParamDto queryParam = JSON.parseObject(param,PatientParamDto.class);
            boolean flag = (queryParam == null ||  ( queryParam != null &&
                    StringUtils.isEmpty(queryParam.getPatientId()) &&
                    StringUtils.isEmpty(queryParam.getPatientNo()) &&
                    StringUtils.isEmpty(queryParam.getInPatientId()) &&
                    StringUtils.isEmpty(queryParam.getInPatientNo())));
            if(flag){
                throw new WebServiceException(ErrorCodeEnums.ERROR_EMPTY_PARAME);
            }else{
                if(StringUtils.isEmpty(queryParam.getPatientId()) && StringUtils.isEmpty(queryParam.getPatientType())){
                    throw new WebServiceException(ErrorCodeEnums.ERROR_EMPTY_PARAME);
                }
            }
            HisImpl his = new HisImpl();
            List<PatientResultDto> list = his.getHisPatientById(queryParam.getOrgId(),queryParam.getPatientType(),queryParam.getPatientId(),queryParam.getPatientNo(),
                    queryParam.getInPatientId(),queryParam.getInPatientNo());
            if(list != null){
                msg = ReturnMsg.success(Constants.YesNo.YES,list);
            }
        } catch (Exception e) {
            log.error(e);
            throw new WebServiceException(ErrorCodeEnums.ERROR_SYSTEM.getErrorCode(),e.getMessage());
        }
        return msg;
    }

    /**
     * 1009:根据病区获取所有病人信息
     * @param param 参数
     * @return 根据病区获取所有病人信息
     */
    public ReturnMsg getPatientInfoByWard(String orgId,String param){
        ReturnMsg msg = ReturnMsg.success(Constants.YesNo.NO,"获取病区病人信息失败");
        try {
            PatientParamDto queryParam = JSON.parseObject(param,PatientParamDto.class);
            boolean flag = (queryParam ==null || (queryParam!= null &&
                    StringUtils.isEmpty(queryParam.getWardCode())));
            if(flag){
                throw new  WebServiceException(ErrorCodeEnums.ERROR_EMPTY_PARAME);
            }
            HisImpl his = new HisImpl();
            List<PatientResultDto> list = his.getHisPatientByWard(queryParam.getOrgId(),queryParam.getPatientType(),queryParam.getWardCode());
            if(list != null && list.size() > 0){
                msg = ReturnMsg.success(Constants.YesNo.YES,list);
            }
        }catch (Exception e){
            log.error(e);
            throw new WebServiceException(ErrorCodeEnums.ERROR_SYSTEM.getErrorCode(),e.getMessage());
        }
        return msg;
    }

    /***
     * 1002:获取HIS病区/科室信息
     * @param param 参数
     * @return 获取HIS病区/科室信息
     */
    public ReturnMsg getWardInfo(String orgId,String param){
        ReturnMsg msg = ReturnMsg.success(Constants.YesNo.NO,"获取病区科室信息失败");
        try {
            DepartmentParamDto queryParam = JSON.parseObject(param,DepartmentParamDto.class);
            boolean flag = (queryParam ==null || (queryParam!= null && StringUtils.isEmpty(queryParam.getOrgId())));
            if(flag){
                throw new  WebServiceException(ErrorCodeEnums.ERROR_EMPTY_PARAME);
            }
            HisImpl his = new HisImpl();
            List<DepartmentResultDto> departmentList = his.getWardList();
            if(departmentList != null && his.getWardList().size() >0){
                return ReturnMsg.success(Constants.YesNo.YES, his.getWardList());
            }
        }catch (Exception e){
            log.error(e);
            throw new WebServiceException(ErrorCodeEnums.ERROR_SYSTEM.getErrorCode(),e.getMessage());
        }
        return msg;
    }

    /***
     * 1003:获取HIS医生/护士信息
     * @param param 参数
     * @return 获取HIS医生/护士信息
     */
    public ReturnMsg getDoctorInfo(String orgId,String param){
        ReturnMsg msg = ReturnMsg.success(Constants.YesNo.NO,"获取医生信息失败");
        try {
            DoctorParamDto queryParam = JSON.parseObject(param,DoctorParamDto.class);
            boolean flag = (queryParam == null || (queryParam!= null && StringUtils.isEmpty(queryParam.getOrgId())));
            if(flag){
                throw new  WebServiceException(ErrorCodeEnums.ERROR_EMPTY_PARAME);
            }
            HisImpl his = new HisImpl();
            List<DoctorResultDto> doctorDtoList = his.getDoctorList(queryParam.getOrgId(),queryParam.getDoctorCode());
            if(doctorDtoList != null && doctorDtoList.size() >0){
                msg = ReturnMsg.success(Constants.YesNo.YES,doctorDtoList);
            }
        }catch (Exception e){
            log.error(e);
            throw new WebServiceException(ErrorCodeEnums.ERROR_SYSTEM.getErrorCode(),e.getMessage());
        }
        return msg;
    }
}
